/**
 * Created by Mark_ren on 2017/5/23.
 * 今天心情不好，可能是因为阴天。
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');
var nodeExcel = require('excel-export');

var sortSubscribeLogController = {

    /**
     *  获取点击记录
     * @param req
     * @param res
     */
    getSortSubscribeLog: function (req, res) {
        co(function () {
            var paramD = [];
            var params = {};
            var paramT = {};
            var include = [{ model: seq.dictionaryModule, attributes: ['codeDesc'] }, { model: seq.memberModule, attributes: ['name', 'tel', 'hospitalName', 'hospitalOffices'] }]
            paramT.include = include;
            if (req.query.startTime != undefined) {
                var paramU = {};
                paramU.createTime = { "$gt": req.query.startTime };
                paramD.push(paramU);
            }
            if (req.query.endTime != undefined) {
                var paramU = {};
                paramU.createTime = { "$lt": req.query.endTime };
                paramD.push(paramU);
            }
            if (req.query.startTime != undefined || req.query.endTime != undefined) {
                params["$and"] = paramD;
            }
            paramT.where = params;
            paramT.order = [["createTime", "DESC"]];
            var page = parseInt(req.query.page);
            if (req.query.page != undefined && req.query.rows != undefined) {
                paramT.offset = page;
                paramT.limit = parseInt(req.query.rows);
            }
            seq.sortSubscribeLogModule.findAndCountAll(paramT).then(function (result) {
                res.json({
                    success: true,
                    message: "查询成功！",
                    total: result.count,
                    page: page,
                    result: result.rows
                });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    },
    exportSortSubscribeLog: function (req, res) {
        co(function () {
            var paramD = [];
            var params = {};
            var paramT = {};
            var include = [{ model: seq.dictionaryModule, attributes: ['codeDesc'] }, { model: seq.memberModule, attributes: ['name', 'tel', 'hospitalName', 'hospitalOffices'] }]
            paramT.include = include;
            if (req.query.startTime != undefined) {
                var paramU = {};
                paramU.createTime = { "$gt": req.query.startTime };
                paramD.push(paramU);
            }
            if (req.query.endTime != undefined) {
                var paramU = {};
                paramU.createTime = { "$lt": req.query.endTime };
                paramD.push(paramU);
            }
            if (req.query.startTime != undefined || req.query.endTime != undefined) {
                params["$and"] = paramD;
            }
            paramT.where = params;
            paramT.order = [["createTime", "DESC"]];

            seq.sortSubscribeLogModule.findAll(paramT).then(function (result) {
                var conf = {};
                conf.stylesXmlFile = "styles.xml";//注意：这个文件需要去我刚刚发的那个网址上下载一个下来
                conf.name = "sortSubscribeLog";
                conf.cols = [
                    {
                        caption: '姓名',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '医院',
                        type: 'string',
                        width: 10
                    }, {
                        caption: '科室',
                        type: 'string',
                        width: 25
                    }, {
                        caption: '栏目名称',
                        type: 'string',
                        width: 25
                    },
                    {
                        caption: '点击时间',
                        type: 'string',
                        width: 10
                    }
                ];


                var vac = new Array();
                for (var i = 0; i < result.length; i++) {
                    var val = result[i];
                    var temp = new Array();
                    if (val.member != undefined && val.member != null) {
                        temp[0] = val.member.name;
                        temp[1] = val.member.hospitalName;
                        temp[2] = val.member.hospitalOffices;
                    } else {
                        temp[0] = "";
                        temp[1] = "";
                        temp[2] = "";
                    }
                    if (val.dictionary != undefined && val.dictionary != null) {
                        temp[3] = val.dictionary.codeDesc;
                    }
                    temp[4] = val.createTime || "";

                    vac.push(temp);

                }
                conf.rows = vac;//conf.rows只接受数组
                var results = nodeExcel.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + "itemSubscribeExport.xlsx");
                res.end(results, 'binary');
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })

    }
}
module.exports = sortSubscribeLogController;